/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.peng.service.impl;

import me.peng.domain.Findthum;
import me.peng.utils.ValidationUtil;
import me.peng.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.peng.repository.FindthumRepository;
import me.peng.service.FindthumService;
import me.peng.service.dto.FindthumDto;
import me.peng.service.dto.FindthumQueryCriteria;
import me.peng.service.mapstruct.FindthumMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.peng.utils.PageUtil;
import me.peng.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author pengjianye
* @date 2021-10-14
**/
@Service
@RequiredArgsConstructor
public class FindthumServiceImpl implements FindthumService {

    private final FindthumRepository findthumRepository;
    private final FindthumMapper findthumMapper;

    @Override
    public Map<String,Object> queryAll(FindthumQueryCriteria criteria, Pageable pageable){
        Page<Findthum> page = findthumRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(findthumMapper::toDto));
    }

    @Override
    public List<FindthumDto> queryAll(FindthumQueryCriteria criteria){
        return findthumMapper.toDto(findthumRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public FindthumDto findById(Integer id) {
        Findthum findthum = findthumRepository.findById(id).orElseGet(Findthum::new);
        ValidationUtil.isNull(findthum.getId(),"Findthum","id",id);
        return findthumMapper.toDto(findthum);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FindthumDto create(Findthum resources) {
        return findthumMapper.toDto(findthumRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Findthum resources) {
        Findthum findthum = findthumRepository.findById(resources.getId()).orElseGet(Findthum::new);
        ValidationUtil.isNull( findthum.getId(),"Findthum","id",resources.getId());
        findthum.copy(resources);
        findthumRepository.save(findthum);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            findthumRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<FindthumDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FindthumDto findthum : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", findthum.getParetid());
            map.put("动态id", findthum.getFindid());
            map.put("是否点赞0/1", findthum.getTothums());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
